package jp.ac.segakuen.tags.opencampas;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.JspWriter;
import jp.ac.segakuen.PMF;
import jp.ac.segakuen.OpenCampassMenuTable;
import jp.ac.segakuen.SelectListTable;

import java.util.List;

import javax.jdo.PersistenceManager;

public class ocSelectTagServlet implements Tag {
	private PageContext pageContext;
	private Tag parentTag;
	
	public int doEndTag() throws JspException {
	     return EVAL_PAGE;
	}

	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "select from jp.ac.segakuen.SelectListTable where displayPlaceHolder == 'ocdate' order by sortOrder ASC";
		List<SelectListTable> selectlist = (List<SelectListTable>) pm.newQuery(query).execute();

		try {
	       JspWriter out = pageContext.getOut();
			for (SelectListTable l : selectlist) {
				//String id = l.getStringId();
				String id = l.getListText();
				out.print("bunruiB[\"");
				out.print(id);
				out.print("\"]= new Array(");
				query = "select from jp.ac.segakuen.OpenCampassMenuTable where ocDateId =="+l.getStringId();
				List<OpenCampassMenuTable> results = (List<OpenCampassMenuTable>) pm.newQuery(query).execute();
			    	for(OpenCampassMenuTable ocmt : results){
			    		out.print("\"");
			    		out.print(ocmt.getOcMenuID());
			    		out.print("\"");
			    		if(results.iterator().hasNext()){
			    			out.print(",");
			    		}
			    	}
	        out.println("\"\");");
			}
	      } catch(Exception e) {
	        throw new JspException(e.getMessage());
	      }
	        return SKIP_BODY;
		
	}

	public Tag getParent() {
		return this.parentTag;
	}

	public void release() {


	}

	public void setPageContext(PageContext pageContext) {

		this.pageContext = pageContext;
	}

	public void setParent(Tag parentTag) {

		this.parentTag = parentTag;
	}

}
